<?php

namespace App\Exports;

use App\Rep\OrderRep;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\FromCollection;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class ExternalErrorReportExport implements FromCollection,WithStyles
{
    private $row;
    private $params;

    public function __construct($row,$params)
    {
        $this->row = $row;
        $this->params = $params;
    }

    public function collection()
    {
        $params = $this->params;
        $rep = new OrderRep();
        $invoices = $rep->getOrderListRep($params);
        $array = $invoices->toArray()['data'];
        //p($data);

//        //设置表头
//        foreach ($row[0] as $key => $value) {
//            $key_arr[] = $key;
//        }
//
//        //输入数据
//        foreach ($data as $key => &$value) {
//            $js = [];
//            for ($i=0; $i < count($key_arr); $i++) {
//                $js = array_merge($js,[ $key_arr[$i] => $value[ $key_arr[$i] ] ]);
//            }
//            array_push($row, $js);
//            unset($val);
//        }

        foreach ($array as $key=>&$val){
            $data[$key]['id'] = $val['id'];
            $data[$key]['order_number'] = $val['order_number']??"";
            $data[$key]['customer_name'] = $val['customer']['customer_name']??'';
            $data[$key]['batch_number'] = $val['batch_number']??'';
            $data[$key]['serial_number'] = $val['serial_number']??'';
            $data[$key]['product_name'] = $val['customer']['product_name']??'';
            $data[$key]['model_number'] = $val['model_number']['model_number']??'';
            $data[$key]['specification'] = $val['specification']['specification']??'';
            $data[$key]['color'] = $val['color']['color']??'';
            $data[$key]['direction'] = $val['direction']??'';
            $data[$key]['remark'] = $val['remark']??'';
            $data[$key]['order_date'] = $val['order_date']??'';
            $data[$key]['delivery_date'] = $val['delivery_date']??'';
        }
        //p($data);
        return collect($data);
    }

    public function styles(Worksheet $sheet)
    {
        // 合并单元格
        $sheet->mergeCells('A1:E1');
        // 设置单元格的值
        $sheet->getCell('A1')->setValue("填写须知：\n 1. 请勿修改表格结构；\n 2. 标红字段为必填项，黑色字段为选填项；");
        // 设置字体颜色
        $sheet->getStyle('A5')->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
        $sheet->getStyle('B5')->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
        $sheet->getStyle('F')->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
        // 设置背景色
        $sheet->getStyle('A5:E5')->getFill()
            ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
            ->getStartColor()->setRGB('e9e9e9');
        // 设置列宽
        $sheet->getColumnDimension('A')->setWidth(20);
        $sheet->getColumnDimension('B')->setWidth(20);
        $sheet->getColumnDimension('C')->setWidth(20);
        $sheet->getColumnDimension('D')->setWidth(30);
        $sheet->getColumnDimension('E')->setWidth(20);
        $sheet->getColumnDimension('F')->setWidth(20);
        // 设置行高
        $sheet->getRowDimension('1')->setRowHeight(100);
        $sheet->getRowDimension('2')->setRowHeight(1);
        $sheet->getRowDimension('3')->setRowHeight(1);
        $sheet->getRowDimension('4')->setRowHeight(1);
        // 设置默认行高
        $sheet->getDefaultRowDimension()->setRowHeight(17);
        // setWrapText自动换行，setVertical垂直对齐方式
        $sheet->getStyle('A1')->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP)->setWrapText(true);

        // 锁定单元格
        $sheet->getProtection()->setSheet(true);
        // 这里原本只想让这几个单元格不可编辑，但是不知道怎么就所有都不能编辑了，等找到更好的办法会再解决这里
        $sheet->getStyle('A1:F5')->getProtection()->setLocked(\PhpOffice\PhpSpreadsheet\Style\Protection::PROTECTION_PROTECTED);
        // 注意，这里要将可编辑的地方设为不受保护，不然整个excel都不能编辑了
        $sheet->getStyle('A6:F150')->getProtection()->setLocked(\PhpOffice\PhpSpreadsheet\Style\Protection::PROTECTION_UNPROTECTED);

    }
}